<template>
  <div class="payment-detail">
    <div class="page-header">
      <h2>支付订单详情</h2>
      <el-button @click="$router.back()">返回</el-button>
    </div>

    <el-card class="detail-card">
      <template #header>
        <div class="card-header">
          <span>订单信息</span>
        </div>
      </template>

      <el-descriptions :column="2" border>
        <el-descriptions-item label="订单编号">{{ orderInfo.orderNo }}</el-descriptions-item>
        <el-descriptions-item label="支付金额">¥{{ orderInfo.amount }}</el-descriptions-item>
        <el-descriptions-item label="支付类型">
          <el-tag :type="orderInfo.paymentType === 1 ? 'success' : 'primary'">
            {{ orderInfo.paymentType === 1 ? '支付宝' : '微信支付' }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="订单状态">
          <el-tag :type="getStatusType(orderInfo.status)">
            {{ getStatusText(orderInfo.status) }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="用户ID">{{ orderInfo.userId }}</el-descriptions-item>
        <el-descriptions-item label="司机ID">{{ orderInfo.driverId }}</el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ formatDateTime(orderInfo.createTime) }}</el-descriptions-item>
        <el-descriptions-item label="更新时间">{{ formatDateTime(orderInfo.updateTime) }}</el-descriptions-item>
      </el-descriptions>

      <div class="action-buttons" v-if="orderInfo.status === 1">
        <el-button type="primary" @click="handleRefund">申请退款</el-button>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import axios from 'axios'

const route = useRoute()
const router = useRouter()
const orderInfo = ref({})

// 获取订单详情
const getOrderInfo = async () => {
  try {
    const orderNo = route.params.orderNo
    const response = await axios.get(`/api/payment/query/${orderNo}`)
    orderInfo.value = response.data
  } catch (error) {
    ElMessage.error('获取订单详情失败')
  }
}

// 获取状态类型
const getStatusType = (status) => {
  const types = {
    0: 'warning',  // 未支付
    1: 'success',  // 已支付
    2: 'danger'    // 已取消
  }
  return types[status] || 'info'
}

// 获取状态文本
const getStatusText = (status) => {
  const texts = {
    0: '未支付',
    1: '已支付',
    2: '已取消'
  }
  return texts[status] || '未知状态'
}

// 格式化日期时间
const formatDateTime = (dateTime) => {
  if (!dateTime) return '-'
  return new Date(dateTime).toLocaleString()
}

// 处理退款申请
const handleRefund = () => {
  router.push({
    path: '/refund/create',
    query: {
      orderNo: orderInfo.value.orderNo,
      amount: orderInfo.value.amount
    }
  })
}

onMounted(() => {
  getOrderInfo()
})
</script>

<style scoped>
.payment-detail {
  padding: 20px;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.detail-card {
  max-width: 800px;
  margin: 0 auto;
}

.card-header {
  font-size: 16px;
  font-weight: bold;
}

.action-buttons {
  margin-top: 20px;
  text-align: center;
}
</style> 